<?php

namespace app\model;

use think\facade\Session;
use think\Model;
use think\model\concern\SoftDelete;

/**
 * @mixin think\Model
 */
class User extends Model
{
  //

  use SoftDelete;

  protected static $modelUser = [];

  public $readUser = null;

  public static $typeName = [
    0 => '普通用户',
    1 => '管理员'
  ];
  public static $authStatusName = [
    0 => '未认证',
    1 => '已认证',
    2 => '认证撤回',
    3 => '审核中',
    4 => '已驳回',
  ];

  protected $defaultSoftDelete = 0;

  public function vip()
  {
    return $this->belongsTo(Vip::class,'vip_id');
  }

  public function friends()
  {
    return $this->hasMany(UserFriend::class, 'user_id');
  }

  public function getAvatarSrcAttr()
  {

    $value = $this->getData('avatar');

    if (empty($value)) {
      $value = '/static/images/avatar.jpeg';
    }

    return \get_source_link($value);
  }
  public function getIncomeQrcodeWechatSrcAttr()
  {

    $value = $this->getData('income_qrcode_wechat');

    if (empty($value)) {
      $value = '/static/images/upload.png';
    }

    return \get_source_link($value);
  }
  public function getIncomeQrcodeAlipaySrcAttr()
  {

    $value = $this->getData('income_qrcode_alipay');

    if (empty($value)) {
      $value = '/static/images/upload.png';
    }

    return \get_source_link($value);
  }

  public function getTypeNameAttr()
  {
    return self::$typeName[$this->getData('type')];
  }
  public function getAuthStatusNameAttr()
  {
    return self::$authStatusName[$this->getData('auth_status')];
  }


  public function getIndexUrlAttr()
  {
    return url('User/index', ['id' => $this->getData('id')]);
  }

  public static function getCurrentUser()
  {

    $user_id = Session::get('user_id');

    if (empty($user_id)) {
      return null;
    }

    if (isset(self::$modelUser[$user_id])) {
      return self::$modelUser[$user_id];
    }

    self::$modelUser[$user_id] = User::find($user_id);

    return self::$modelUser[$user_id];
  }

  public function getIsFriendAttr()
  {
    if (is_null($this->readUser)) {
      return false;
    }
    return UserFriend::isFriend($this->readUser->id, $this->getData('id'));
  }

}
